<?php
namespace app\api\controller;
/**
 * @Author: 刘洪
 * @Date:   2019-03-30 09:49:00
 * @Last Modified by:   user
 * @Last Modified time: 2019-04-04 15:11:22
 */
use think\Controller;
use think\Db;
use think\Cache;
use app\api\model\ApiAuthorizationModel;
use app\admin\model\AdminMenuModel;
use app\admin\model\AccessModel;
use app\admin\model\ModuleMenuModel;
/**
 * 授权api接口类
 */
class ApiAuthorizationController extends Controller
{
	/**
	 * 检查授权
	 * @param ACCESSID  标识
	 * @return [type] [description]
	 */
	function _initialize()
	{
		if(!$this->request->isPost()){
			echo zy_json(false,'请求类型错误！');exit;
		}
		//调用登入token验证方法
		$data=$this->request->param();
		if(!isset($data['token']) || empty($data['token']) ){
			echo zy_json(false,'缺省必要的参数值，请检查！');exit;
		}
		if(!isset($data['token']['ACCESSID']) || empty($data['token']['ACCESSID']) ){
			echo zy_json(false,'缺省必要的参数值，请检查-C！');exit;
		}
		if(!isset($data['token']['KEYID']) || empty($data['token']['KEYID']) ){
			echo zy_json(false,'缺省必要的参数值，请检查-K！');exit;
		}
		if(!isset($data['token']['ADMINID']) || empty($data['token']['ADMINID']) ){
			echo zy_json(false,'缺省必要的参数值，请检查-A！');exit;
		}
		$data['token']['ADMINID']=base64_decode($data['token']['ADMINID']);

		$accessModel=new AccessModel();
		$res=$accessModel->checkToke($data['token']);
		if(!$res){
			echo zy_json(false,'非法请求');exit;
		}else if(1===$res){
			//输出错误信息
			echo zy_json(false,'公司账户异常，已被禁用,无法获取信息！');exit;
		}else if(3===$res){
			//输出错误信息
			echo zy_json(false,'公司帐号未被激活！');exit;
		}else if(2===$res){
			echo zy_json(false,'公司账号到期！');exit;
		}
	}
	public  function test(){
		$authM=new ApiAuthorizationModel();
		$res=$authM->getAccessMenus(173);
		dump($res);
	}
	/**
	 * 保存模块菜单
	 * @param  $menu   加入到admin_menu中的菜单
	 * @param  $companyId  公司的id
	 * @return  返回新增的模块菜单id
	 */
	public function saveModuleMenu( $menu=null , $moduleId = null )
	{
		$data=$this->request->param();
		$menu = isset($data['menu']) ? $data['menu'] : $menu;
		$moduleId = isset($data['moduleId'])?$data['moduleId']:$moduleId;
		if( empty( $menu )  ){
			return zy_json_echo( false,'未正确传递参数,请检查！' );
		}
		$moduleVersion = null ;
		//兼容子系统低版本
		if( !empty( $moduleId ) ){
			$moduleVersion = Db::name('module_store')->where( 'id' , $moduleId )->value('version');//菜单版本
		}
		
		
		//将菜单加入到 admin_menu 数据表
		$adminM = new AdminMenuModel();
		$menu = $adminM->saveMenu( $menu , $moduleVersion );
		if( $menu['mainMenu'] == -1 ){
			echo -1;exit;
		}
		//保存到模块菜单20190927
		$accessModel = new AccessModel();
		$userInfo = $accessModel->getUserInfo( base64_decode( $data['token']['ADMINID'] ) );
		$moduleM = new ModuleMenuModel();
		$res = $moduleM->saveMenu( $userInfo['company_id'] , $menu , $moduleId );
		if( $res >0 ){
			echo $menu['mainMenu'];exit;
		}
		echo -1;exit;
	}
	
	//子系统1.1新增 更新菜单2020316
	public function updateModuleMenu( $menu = null , $moduleId = null )
	{
		$data=$this->request->param();
		$menu = isset($data['menu'])?$data['menu']:$menu;
		$moduleId = isset($data['moduleId'])?$data['moduleId']:$moduleId;
		if( empty($menu )  || empty( $moduleId ) ){
			return zy_json_echo( false,'未正确传递参数,请检查！' );
		}
		
		//将菜单加入到 admin_menu 数据表
		$adminM = new AdminMenuModel();
		//更新菜单不加入菜单库，菜单版本为空
		$menu = $adminM->saveMenu( $menu );
		if( $menu['mainMenu'] == -1 ){
			echo -1;exit;
		}
		//保存到模块菜单 新版子系统更新的菜单 统一标识为定制菜单  cmf_module_menu 添加字段 custom
		$accessModel = new AccessModel();
		$userInfo = $accessModel->getUserInfo( base64_decode( $data['token']['ADMINID'] ) );
		$moduleM = new ModuleMenuModel();
		//param (公司id , 菜单 ，模块id ，是定制菜单)
		$res = $moduleM->saveMenu( $userInfo['company_id'] , $menu , $moduleId , 1 );
		if( $res >0 ){
			echo $menu['mainMenu'];exit;
		}
		echo -1;exit;
		
	}
	
	
	/**
	 * 删除模块菜单
	 * @return [type] [description]20190927
	 */
	public function deleteModuleMenu( $moduleMenuId = null )
	{
		$data = $this->request->param();
		$moduleMenuId = isset($data['moduleMenuId'])?$data['moduleMenuId']:$moduleMenuId;
		$accessModel = new AccessModel();
		$userInfo = $accessModel->getUserInfo(base64_decode($data['token']['ADMINID']));
		if( empty( $moduleMenuId ) ){
			return zy_json_echo( false , '未正确传递参数,请检查！' );
		}
		$moduleM = new ModuleMenuModel();
		$res = $moduleM->deleteMenuPlus( $moduleMenuId , $userInfo['company_id'] );
		echo $res; exit;
	}


	/**
	 * 读取授权菜单方法
	 * @param  userInfo  用户信息 子账号所保存的信息
	 * @return  返回菜单那
	 */
	public function getAdminMenus()
	{
		$data=$this->request->param();
		$adminId=base64_decode($data['token']['ADMINID']);
		$authM=new ApiAuthorizationModel();
		$res=$authM->getAccessMenus($adminId);
		echo zy_json(true,'',$res);exit;
	}

	/**
	 * 超管修改密码
	 */
	public function modifyPwd(){
		$data=$this->request->param();
		$adminId=base64_decode($data['token']['ADMINID']);
		$password=base64_decode($data['password']);
		$res=Db::name('user')->where('id',$adminId)->update(['user_pass'=>cmf_password($password)]);
		return zy_json_echo(true,'成功！'.$password."  ".$adminId);
		if(!$res){
			return zy_json_echo(false,'密码修改失败！',null,-1);
		}
		return zy_json_echo(true,'成功！');
	}
	/**
	 * 超管修改密码
	 */
	public function modifyPwdCms(){
		$data=$this->request->param();
		$adminId=base64_decode($data['token']['ADMINID']);
		$password=base64_decode($data['password']);
		$res=Db::name('user')->where('id',$adminId)->update(['user_pass'=>$password]);
		if(!$res){
			return zy_json_echo(false,'密码修改失败！',null,-1);
		}
		return zy_json_echo(true,'成功！');
	}
}